package com.vWare.documentparser.Controls;

import java.util.ArrayList;
import java.util.List;

import Items.JustifyedTextLine;
import Items.TextAlignment;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.widget.TextView;

public class ExtendedTextView extends TextView
{

	private TextAlignment _TextAlignment = TextAlignment.Left;  

	public ExtendedTextView(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);
	}

	public ExtendedTextView(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
	}

	public ExtendedTextView(Context context) 
	{
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas)
	{

		switch (_TextAlignment)
		{
		case LeftJustified:
			_drawTextLeftJustified(canvas);
			break;
		case RightJustified:
			break;
		case CenterJustified:
			break;
		case Left:
			_drawTextLeft(canvas);
			break;
		case Right:
			//super.getPaint().setTextAlign(Align.RIGHT);
			super.onDraw(canvas);
			break;
		case Center:
			//super.getPaint().setTextAlign(Align.CENTER);
			super.onDraw(canvas);
			break;
		default:
			super.getPaint().setTextAlign(Align.LEFT);
			super.onDraw(canvas);
			break;
		}



	}

	// -------------------------------------------------------------------------------------------------------------------------------------------------
	// Left Alignment.
	// -------------------------------------------------------------------------------------------------------------------------------------------------	
	private void _drawTextLeft(Canvas canvas)
	{

		List<JustifyedTextLine> liTxtToDraw = leftText(this.getText().toString());
		int yLine = super.getLineHeight();


		super.getPaint().setColor(super.getCurrentTextColor());


		for (int i = 0; i < liTxtToDraw.size(); ++i)
		{

			canvas.drawText(liTxtToDraw.get(i).getText(), 0, yLine, super.getPaint());
			yLine += super.getLineHeight();
		}	




		if (this.getHeight() != yLine)
			this.setHeight(yLine);

	}

	private List<JustifyedTextLine> leftText(String input)
	{

		// A single line of text.
		String txtSigleLine = "";
		// The input string split into single sentences.
		String[] atxtInputSplitted;
		// The array which will contains the lines.
		ArrayList<JustifyedTextLine> liReturn = new ArrayList<JustifyedTextLine>();

		atxtInputSplitted = input.split(" ");

		for (int i = 0; i < atxtInputSplitted.length; ++i)
		{


			if (LineReady(txtSigleLine) > super.getWidth()) 
			{

				// Makes sure that we don't have any spaces in front of the string.
				txtSigleLine = txtSigleLine.trim();
				// Calculates the space between each sentence.
				Float fSpace = (super.getWidth() - super.getPaint().measureText(txtSigleLine.replace(" ", "")) - this.getPaddingLeft() - this.getPaddingRight()) / (txtSigleLine.split(" ").length - 1);
				// Add the line to our array.
				liReturn.add(new JustifyedTextLine(txtSigleLine, fSpace, false));

				// Resets the text line.
				txtSigleLine = "";
				txtSigleLine = txtSigleLine + " " + atxtInputSplitted[i];


			}
			else if (atxtInputSplitted[i].equals("\n"))
			{

				// Makes sure that we don't have any spaces in front of the string.
				txtSigleLine = txtSigleLine.trim();
				// Add the line to our array.								
				liReturn.add(new JustifyedTextLine(txtSigleLine, 0f, true));

				txtSigleLine = "";

			}
			else
			{

				txtSigleLine = txtSigleLine + " " + atxtInputSplitted[i];
				txtSigleLine = txtSigleLine.trim();
			}

		}

		if (txtSigleLine.length() != 0)
		{
			// Makes sure that we don't have any spaces in front of the string.
			txtSigleLine = txtSigleLine.trim();
			// Add the line to our array.
			liReturn.add(new JustifyedTextLine(txtSigleLine, 0f, true));

			txtSigleLine = "";
		}


		return liReturn;
	}

	// -------------------------------------------------------------------------------------------------------------------------------------------------
	// Left Justified Alignment. 
	// -------------------------------------------------------------------------------------------------------------------------------------------------	
	private List<JustifyedTextLine> leftjustifyText(String input)
	{

		// A single line of text.
		String txtSigleLine = "";
		// The input string split into single sentences.
		String[] atxtInputSplitted;
		// The array which will contains the lines.
		ArrayList<JustifyedTextLine> liReturn = new ArrayList<JustifyedTextLine>();

		atxtInputSplitted = input.split(" ");

		for (int i = 0; i < atxtInputSplitted.length; ++i)
		{


			if (LineReady(txtSigleLine + " " + atxtInputSplitted[i]) > super.getWidth()) 
			{

				// Makes sure that we don't have any spaces in front of the string.
				txtSigleLine = txtSigleLine.trim();
				// Calculates the space between each sentence.
				Float fSpace = (super.getWidth() - super.getPaint().measureText(txtSigleLine.replace(" ", "")) - this.getPaddingLeft() - this.getPaddingRight()) / (txtSigleLine.split(" ").length - 1);
				// Add the line to our array.
				liReturn.add(new JustifyedTextLine(txtSigleLine, fSpace, false));

				// Resets the text line.
				txtSigleLine = "";
				txtSigleLine = txtSigleLine + " " + atxtInputSplitted[i];


			}
			else if (atxtInputSplitted[i].equals("\n"))
			{

				// Makes sure that we don't have any spaces in front of the string.
				txtSigleLine = txtSigleLine.trim();
				// Add the line to our array.								
				liReturn.add(new JustifyedTextLine(txtSigleLine, 0f, true));

				txtSigleLine = "";

			}
			else
			{

				txtSigleLine = txtSigleLine + " " + atxtInputSplitted[i];
				txtSigleLine = txtSigleLine.trim();
			}

		}

		if (txtSigleLine.length() != 0)
		{
			// Makes sure that we don't have any spaces in front of the string.
			txtSigleLine = txtSigleLine.trim();
			// Add the line to our array.
			liReturn.add(new JustifyedTextLine(txtSigleLine, 0f, true));

			txtSigleLine = "";
		}


		return liReturn;
	}

	private void _drawTextLeftJustified(Canvas canvas)
	{

		List<JustifyedTextLine> liTxtToDraw = leftjustifyText(this.getText().toString());
		int yLine = super.getLineHeight();
		float fXPos = 0;

		super.getPaint().setColor(super.getCurrentTextColor());


		for (int i = 0; i < liTxtToDraw.size(); ++i)
		{

			if (liTxtToDraw.get(i).getIsLastLine())
			{
				canvas.drawText(liTxtToDraw.get(i).getText(), fXPos, yLine, super.getPaint());
			}
			else
			{

				for (int ii = 0; ii < liTxtToDraw.get(i).getText().split(" ").length; ++ii)
				{
					canvas.drawText(liTxtToDraw.get(i).getText().split(" ")[ii], fXPos, yLine, super.getPaint()); 
					fXPos += super.getPaint().measureText(liTxtToDraw.get(i).getText().split(" ")[ii]) + liTxtToDraw.get(i).getSpaceSize();
				}
			}	

			fXPos = 0;
			yLine += super.getLineHeight();

		}

		if (this.getHeight() != yLine)
			this.setHeight(yLine);

	}

	// -------------------------------------------------------------------------------------------------------------------------------------------------
	// General Functions.
	// -------------------------------------------------------------------------------------------------------------------------------------------------
	private float LineReady(String input)
	{


		String txtInputWOS = input.replace(" ", "");
		float fStrWidthWOS = this.getPaint().measureText(txtInputWOS);
		float fSpaceSize = (super.getWidth() - (fStrWidthWOS - (super.getPaddingLeft() + super.getPaddingRight()))) / (input.split(" ").length);

		if (fSpaceSize <= 9)
		{
			fSpaceSize += 1;
		}



		return (fStrWidthWOS + (input.split(" ").length ) * fSpaceSize);


	}

	// -------------------------------------------------------------------------------------------------------------------------------------------------
	// Properties.
	// -------------------------------------------------------------------------------------------------------------------------------------------------
	public TextAlignment getTextAlignment()
	{
		return _TextAlignment;
	}

	public void setTextAlignment(TextAlignment textalignment)
	{
		_TextAlignment = textalignment;
	}

}